package com.ruike.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ruike.pojo.AcademicBuilding;
import com.ruike.service.AcademicBuildingService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 吴泽胜
 * @FileName AcademicBuildingController
 * @date 2021-01-18 3:00 下午
 * @Software: IntelliJ IDEA
 */
@Controller
@RequestMapping("/academic_building")
public class AcademicBuildingController {

    @Autowired
    private AcademicBuildingService academicBuildingService;

    /**
     * 获取列表信息及数目
     * @param model
     * @return
     * @throws Exception
     */
    @RequiresRoles(value={"Student","Teacher","Admin","Super"})
    @ApiOperation(value = "获取教学楼",notes = "获取教学楼的接口")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String AcademicBuildingUrl(  @RequestParam(defaultValue = "1")Integer pageNum,
                                        @RequestParam(defaultValue="10")Integer pageSize,
                                        @RequestParam(defaultValue="")String searchParam,
                                        Model model ) throws Exception {
        List<AcademicBuilding> academicBuildingList = null;
        if (searchParam.isEmpty()){
            // 是否携带searchParam变量
            academicBuildingList = this.getAcademicBuildingListByPageHelper(pageNum,pageSize);
        }else {
            // 携带参数进行模糊搜索
            academicBuildingList = this.fuzzyAcademicBuildingList(searchParam);
        }
        //查询中最好只有一次查询，不要进行多次查询，pageHelper 默认操作第一次查询的结果集
        PageInfo<AcademicBuilding> info = new PageInfo<AcademicBuilding>(academicBuildingList);
        //将List转换为pageInfo对象，这样可以更方便的在页面中进行分页操作。
        model.addAttribute("pageInfo",info);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", academicBuildingList);
        model.addAttribute("AcademicBuildings",jsonObject);
        return "view/academic_building";
    }

    /**
     * 获取所有信息
     * @return
     * @throws Exception
     */
    @RequiresRoles(value={"Student","Teacher","Admin","Super"})
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<AcademicBuilding> getAcademicBuildingList() throws Exception {
        return academicBuildingService.getAcademicBuildingList();
    }

    /**
     * 通过id修改状态
     * @throws Exception
     */
    @RequiresRoles(value={"Student","Teacher","Admin","Super"})
    @ApiOperation(value = "通过id更新状态",notes = "通过id更新状态的接口")
    @RequestMapping(value = "/updataState/{id}", method = RequestMethod.GET)
    public String updataStateById(@PathVariable("id") Integer id ) throws Exception {
        AcademicBuilding academicBuilding = academicBuildingService.findAcademicBuildingById(id);
        academicBuilding.setState(academicBuilding.getState().equals("启用") ? "禁用" : "启用");
        academicBuildingService.updataStateById(academicBuilding);
        return "redirect:/academic_building";
    }

    /**
     * 通过id删除
     * @throws Exception
     */
    @RequiresRoles(value={"Student","Teacher","Admin","Super"})
    @ApiOperation(value = "通过id删除",notes = "通过id删除的接口")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public String deleteAcademicBuildingById(@PathVariable("id") Integer id) throws Exception {
        academicBuildingService.deleteAcademicBuildingById(id);
        return "redirect:/academic_building";
    }

    /**
     * 更新 academicBuilding
     * @param academicBuilding
     * @return
     * @throws Exception
     */
    @RequiresRoles(value={"Student","Teacher","Admin","Super"})
    @ApiOperation(value = "通过id更新",notes = "通过id更新的接口")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String UpdataAcademicBuildingById(AcademicBuilding academicBuilding) throws Exception {
        if ( !(academicBuilding.getJxldh().isEmpty() && academicBuilding.getJxlmc().isEmpty() && academicBuilding.getState() == null) )
            academicBuildingService.updateAcademicBuildingById(academicBuilding);
        return "redirect:/academic_building";
    }

    /**
     * 保存 AcademicBuilding
     * @param academicBuilding
     * @return
     * @throws Exception
     */
    @RequiresRoles(value={"Student","Teacher","Admin","Super"})
    @ApiOperation(value = "添加教学楼",notes ="添加教学楼的接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String SaveAcademicBuildingById(AcademicBuilding academicBuilding) throws Exception {
        academicBuilding.setRelease_time(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        academicBuildingService.addAcademicBuilding(academicBuilding);
        return "redirect:/academic_building";
    }

    /**
     * 获取分页
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequiresRoles(value={"Student","Teacher","Admin","Super"})
    @ApiOperation(value = "获取分页",notes = "获取分页的接口")
    @RequestMapping(value = "/getlist", method = RequestMethod.GET)
    @ResponseBody
    public List<AcademicBuilding> getAcademicBuildingListByPageHelper(Integer pageNum, Integer pageSize) throws Exception {
        return academicBuildingService.getAcademicBuildingListByPageHelper(pageNum,pageSize);
    }


    /**
     * 模糊搜索 AcademicBuilding 列表
     * @param searchParam
     * @return
     * @throws Exception
     */
    @RequiresRoles(value={"Student","Teacher","Admin","Super"})
    @ApiOperation(value = "模糊搜索 AcademicBuilding 列表",notes = "模糊搜索 AcademicBuilding 列表的接口")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public List<AcademicBuilding> fuzzyAcademicBuildingList(String searchParam) throws Exception {
        return academicBuildingService.fuzzyAcademicBuildingList(searchParam);
    }
    /**
     * 测试
     * @param model
     * @return
     * @throws Exception
     */
    @RequiresRoles(value={"Student","Teacher","Admin","Super"})
    @ApiOperation(value = "测试",notes = "测试")
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public String test(Model model) throws Exception {
        List<AcademicBuilding> academicBuildingList = this.getAcademicBuildingList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", academicBuildingList);
        jsonObject.put("num", academicBuildingList.size());
        return jsonObject.toJSONString();
    }

}
